package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.mapper.RegionMapper;
import cn.wolfcode.trip.base.service.IRegionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionServiceImpl implements IRegionService {
    @Autowired
    private RegionMapper regionMapper;

    public int save(Region region) {

        return regionMapper.insert(region);
    }

    public int delete(Long id)
    {
        return regionMapper.deleteByPrimaryKey(id);
    }

    public Region getByRegionId(Long id) {
        return regionMapper.selectByPrimaryKey(id);
    }

    public int updateByRegionId(Region region) {
        return regionMapper.updateByPrimaryKey(region);
    }

    public List<Region> selectAll(Integer state) {

        return regionMapper.selectAll(state);
    }

    public List<Region> listByParentId(Long parentId) {

        return regionMapper.selectByParentId(parentId);
    }

    public void updateState(Region region) {
        if(region.getState()==0){
            region.setState(1);
        }else {
            region.setState(0);
        }
        regionMapper.updateStateById(region);
    }

    public void saveOrUpdate(Region region) {
        if (region.getId() != null) {
            regionMapper.updateByPrimaryKey(region);
        } else {
            regionMapper.insert(region);
        }
    }


}
